layui.use(['form', 'layer'], function() {
	var form = layui.form,
		layer = layui.layer;

	// 登录过期的时候，跳出ifram框架
	if (top.location != self.location) top.location = self.location;


	// 进行登录操作
	form.on('submit(login)', function(data) {
		data = data.field;
		$.ajax({
			url: 'http://localhost:8080/Online-student/LoginController/getAll',
			type: 'get',
			data: {
				phone: data.phone,
				password: data.password
			},
			dataType: 'json',
			success: function(res) {
				sessionStorage.setItem('user', JSON.stringify(res));
				sessionStorage.setItem('rid', res.data.list[0].rid);
				var rid = res.data.list[0].rid;
				if(rid == 1){
					sessionStorage.setItem('aid', res.data.list[0].aid);
				}else if (rid == 2) {
					sessionStorage.setItem('tid', res.data.list[0].tid);
				}else if (rid == 3) {
					sessionStorage.setItem('sid', res.data.list[0].sid);
				}
				if (res.data == null || Object.keys(res.data).length == 0) {
					layer.msg('用户信息不存在');
					return false;
				}
				if (res.status == 0) {
					if (data.phone == '') {
						layer.msg('用户名不能为空');
						return false;
					}
					let phone = res.data.list[0].phone;
					let password = res.data.list[0].password;
					if (data.phone != phone || data.password != password) {
						layer.msg('用户名或者密码不正确');
						return false;
					}
					if (data.password == '') {
						layer.msg('密码不能为空');
						return false;
					}
					layer.msg('登录成功', function() {
						window.location = 'http://127.0.0.1:8848/onlineStudent-web/index.html';
					});
				}
			}
		})
		return false;
	});
});

